package com.atguigu.spzx.manager.service.impl;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.manager.service.OrederInfoService;
import com.atguigu.spzx.model.dto.order.OrderStatisticsDto;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import com.atguigu.spzx.model.vo.order.OrderStatisticsVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class OrederInfoServiceImpl implements OrederInfoService {
    @Resource
    private OrderStatisticsMapper orderStatisticsMapper;
    @Override
    @Transactional(readOnly = true)
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        //调用OrderStatisticsMapper中带条件查询的方法
        List<OrderStatistics> orderStatisticsList=orderStatisticsMapper.getOrderStatisticsData(orderStatisticsDto);
        //创建OrderStatisticsVo对象
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        //使用stream六实现
        List<String> dateList = orderStatisticsList.stream().map(orderStatistics -> DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd")).collect(Collectors.toList());
        List<BigDecimal> amountList = orderStatisticsList.stream().map(orderStatistics -> orderStatistics.getTotalAmount()).collect(Collectors.toList());
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(amountList);

        return orderStatisticsVo;
    }
}
